package com.caipiao.crawler.football;

        import com.alibaba.fastjson.JSON;
        import com.alibaba.fastjson.JSONArray;
        import com.alibaba.fastjson.JSONObject;
        import com.caipiao.crawler.mapper.LotteryFootballMapper;
        import com.caipiao.crawler.mapper.LotteryTicketMapper;
        import com.caipiao.crawler.model.LotteryFootball;
        import com.caipiao.crawler.model.LotteryTicket;
        import com.caipiao.crawler.zhongjiangRule.FootballRule;
        import com.caipiao.util.DBTools;
        import com.caipiao.util.LotteryUtils;
        import lombok.AllArgsConstructor;
        import lombok.Data;
        import org.apache.commons.collections.CollectionUtils;
        import org.apache.commons.logging.Log;
        import org.apache.commons.logging.LogFactory;
        import org.apache.ibatis.session.SqlSession;
        import us.codecraft.webmagic.ResultItems;
        import us.codecraft.webmagic.Task;
        import us.codecraft.webmagic.pipeline.Pipeline;

        import java.util.ArrayList;
        import java.util.HashMap;
        import java.util.List;
        import java.util.Map;

/**
 * Created by adminitrator on 2018/7/28.
 */
public class FootballResultPipeline implements Pipeline {

    private static Log log = LogFactory.getLog(FootballResultPipeline.class);

    @Data
    @AllArgsConstructor
    public static class Input {
        private double bet;
        private String matchId;
    }

    @Override
    public synchronized void process(ResultItems resultItems, Task task) {
        List<LotteryFootball> m = resultItems.get("modelList");
        log.info("pipe process:" + Thread.currentThread().getId());
        if(m == null) {
            return;
        }
        commit(m);
    }

    public void commit( List<LotteryFootball> list) {
        log.info("-------------FootballResultPipeline commit start------------");
       log.info(JSON.toJSONString(list));
        //TODO 更新开奖结果
        SqlSession session = DBTools.getSession();
        LotteryFootballMapper mapper = session.getMapper(LotteryFootballMapper.class);
        LotteryTicketMapper ticketMapper = session.getMapper(LotteryTicketMapper.class);
        for(int i=0;i<list.size();i++) {
            LotteryFootball football = list.get(i);
            Map map = new HashMap();
            map.put("mid",list.get(i).getMid());
            map.put("status","3");
            int count =  mapper.selectCountByMidAndStatus(map);
            if(count==0) {
                //获取details
                String details = getDetails(mapper, football.getMid());
                football.setPrize_detail(LotteryUtils.getPrizeDetail(
                                details,
                                football.getFirst_half_score(),
                        football.getSecond_half_score(),
                        football.getFirst_scroe(),
                        football.getSecond_score()));
                mapper.updateMatchResult(football);
            }
        }
        session.commit();
        session.close();
        //TODO 触发更新判断是否中奖的逻辑
        FootballRule.footballPrizeRule();
        session.close();
        log.info("-------------FootballResultPipeline commit end------------");
    }

    private String getDetails(LotteryFootballMapper mapper, int mid) {
        List<LotteryFootball> lists = mapper.selectModelByMid(mid+"");
        if(CollectionUtils.isEmpty(lists)) {
            return "";
        }
        return lists.get(0).getDetails();
    }

}
